package packaged.storage;

import play.cache.Cache;

/**
 * 使用Play自带的Cache存储数据
 */
class LocalStorage extends Storage {
    @Override
    public void add(String key, String val) {
        Cache.set(key, val);
    }

    @Override
    public void set(String key, String val) {
        set(key, val, getOvertime());
    }

    @Override
    public void set(String key, String val, String overtime) {
        Cache.set(key, val, overtime);
    }

    @Override
    public void set(String key, String val, int overtime) {
        set(key, val, overtime + "s");
    }

    @Override
    public String get(String key) {
        return get(key,null);
    }

    @Override
    public String get(String key, String spare) {
        Object cache = Cache.get(key);
        if (cache == null)
            return spare;
        return cache.toString();
    }

    @Override
    void del(String key) {
        Cache.delete(key);
    }

    @Override
    String pop(String key) {
        String cache = get(key);
        del(key);
        return cache;
    }

    @Override
    void exp(String key) {
        exp(key,getOvertime());
    }

    @Override
    void exp(String key, String overtime) {
        Cache.set(get(key),overtime);
    }

    @Override
    void exp(String key, int overtime) {
        exp(key, overtime + "s");
    }
}
